<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    // 解决方案--递归2.0
    let data = {
      name: "小灰",
      user: { name: "xiaohui" },
      arr: [],
    };
    // 打印数组和对象的类型都是object
    console.log(typeof data);
    console.log(typeof data.arr);

    // 使用instanceof来区分数组和对象
    console.log(data.arr instanceof Array);

    // 递归
    function copy(object) {
      let res = object instanceof Array ? [] : {};
      for (const [k, v] of Object.entries(object)) {
        res[k] = typeof v === "object" ? copy(v) : v;
      }
      return res;
    }
    let LH = copy(data);
    LH.user.name = "夏晓晓";
    data.arr.push("abc");
    console.log(JSON.stringify(LH, null, 2));
    console.log(JSON.stringify(data, null, 2));
  </script>
</html>
